<template>
	<div class="gk-layout-container">
		<div class="gk-layout-header">
			<s-query perms="doi:item" @click="findPage" add="true" @addClick="handleAdd">
				<s-item v-model="filters.itemId"></s-item>
				<el-form-item>
					<el-input v-model="filters.doNo" @keyup.enter.native="findPage(1)" placeholder="单据编号"
						:style="{ width: '155px' }"></el-input>
				</el-form-item>
				<s-dict name="doType" :min="1" :max="10" v-model="filters.doType"> </s-dict>
				<!-- <el-form-item>
					<el-input v-model="filters.remark"  @keyup.enter.native="findPage(1)"  placeholder="备注" :style="{ width: '145px' }"></el-input>
				</el-form-item> -->
				<s-dict name="doStatus" v-model="filters.status"> </s-dict>
				<s-date @dateChange="function (v) { filters.createBeginTime = v }" placeholder="开始时间" />
				<s-date @dateChange="function (v) { filters.createEndTime = v }" placeholder="结束时间" />

			</s-query>
			<s-reflash reflash="true" excel="true" @findPage="findPage" @exportExcel="exportExcel">
			     <el-tooltip content=" 用友-产成品入库" placement="top">
					<el-button icon="fa fa-database" @click="syncYongyou(2)" />
				</el-tooltip>
				<el-tooltip content=" 用友-采购入库" placement="top">
					<el-button icon="fa fa-database" @click="syncYongyou(4)" />
				</el-tooltip>
				<el-tooltip content=" 用友-其他入库" placement="top">
					<el-button icon="fa fa-database" @click="syncYongyou(3)" />
				</el-tooltip>
			</s-reflash>
		</div>

		<!--表格内容栏-->
		<div class="gk-layout-main">
			<el-table :data="pageResult.content" class='gk-main-table' height="250" v-loading="loading" size="mini"
				align="left" style="width:100%;">
				<el-table-column :label="$t('action.desc')" width="50" header-align="center" align="center">
					<template slot-scope="scope"> {{ scope.$index + 1 }} </template>
				</el-table-column>
				<column prop="doNo" label="入库单号" />
				<column prop="doType" label="入库类型" :formatter="selectionFormat" />
				<column prop="remark" label="备注" />
				<column prop="status" label="状态" :formatter="selectionFormat" />

				<column prop="createTime" :label="$t('beginTime')" :formatter="dateFormat" />
				<column prop="finshTime" :label="$t('finshTime')" :formatter="OverdateFormat" />

				<el-table-column header-align="center" align="center" width="300" :label="$t('action.operation')">
					<template slot-scope="scope">
						<kt-delete-button perms="doi:item:delete"  v-if="scope.row.status ==1"
							@click="handleDelete(scope.$index, scope.row)" />
						<kt-button :label="$t('action.edit')" perms="doi:item:edit"
							@click="handleEdit(scope.$index, scope.row)" />
						<kt-button label="完成" v-if="scope.row.status < 9 && scope.row.status >0" perms="doo:item:edit" @click="finshHandle(scope.$index, scope.row)" />
					</template>
				</el-table-column>


			</el-table>
		</div>
		<!--分页栏-->
		<div class="toolbar gk-layout-footer" style="padding:10px;">

			<el-pagination layout="total, prev, pager, next, jumper" @current-change="findPage"
				:current-page="filters.pageNum" :page-size="filters.pageSize" :total="pageResult.totalSize"
				style="float:right;">
			</el-pagination>
		</div>



		<doi :key="componentKey" ref="editItmeOut" :subDataForm="dataForm" :dialogVisible="dialogVisible1" :add="add"
			:stnOut="this.filters.stn" @receiptHidden="receiptHidden"></doi>

	</div>
</template>

<script>

import KtButton from "@/views/Core/KtButton"
import SDate from "@/components/Kj/SDate"
import SQuery from "@/components/Kj/SQuery"
import SReflash from "@/components/Kj/SReflash"
import SDict from "@/components/Kj/SDict"
import SItem from "@/components/Kj/SItem"
import { format } from "@/utils/datetime"
import Column from "@/views/Core/Column"
import Doi from "./Doi"
import { baseUrl } from '@/utils/global'
import { dictsFormat, getItemName } from "@/utils/myUtil"
import KtDeleteButton from "@/views/Core/KtDeleteButton"
export default {
	components: {
		KtButton,
		SDict,
		SDate,
		SQuery,
		Doi,
		KtDeleteButton,
		Column, SReflash, SItem
	},
	data() {
		return {
			size: 'small',
			filters: {
				doNo: '',
				remark: "",
				outType: 2000,
				pageNum: 1,
				pageSize: this.global.pageSize
			},
			loading: false,
			componentKey: 0,
			baseUrl: baseUrl,


			pageResult: {},

			exportUrl: "receiptOut/ExportExcel",  //导出excel路径
			add: false, // true:新增, false:编辑
			dialogVisible1: false, // 新增编辑界面是否显示
			editLoading: false,
			stockInList: [],
			// 新增编辑界面数据
			dataForm: {
				id: 0,

			},

			deptData: [],
			deptTreeProps: {
				label: 'name',
				children: 'children'
			},
			roles: []

		}
	},
	computed: {

	},
	methods: {
		receiptHidden: function () {

			this.dialogVisible1 = false;
			this.findPage(-1);
		},
		// 获取分页数据
		findPage: function (data) {
			if (data != -1) {
				data = data || 1
				this.filters.pageNum = data

			}

			this.$api.doi.findPage(this.filters).then((res) => {
				this.pageResult = res.data
			})
		},

		// 批量删除
		handleDelete: function (index, data) {
			let params = [{ 'id': data.id }]
			this.$api.doi.batchDelete(params).then((res) => {
				if (res.code == 200) {
					this.$message({ message: '操作成功', type: 'success' })
					this.findPage(this.filters.pageNum)
				} else {
					this.$message({ message: '操作失败, ' + res.msg, type: 'error' })
				}
			})
		},



		handleAdd: function () {

			this.componentKey += 1;
			this.dialogVisible1 = true
			this.add = true
			this.dataForm = {
				id: 0,


			}
		},

		// 显示编辑界面
		handleEdit: function (params, row) {
			this.dialogVisible1 = true
			this.operation = false
			this.dataForm = Object.assign({}, row)
			this.$refs.editItmeOut.getDetials();
		},
		// 时间格式化
		dateFormat: function (row, column, cellValue, index) {
			return format(row[column.property])
		},
		OverdateFormat: function (row, column, cellValue, index) {

			if (row.status >= 9) {
				return format(row[column.property]);
			} else {
				return "";
			}

		},

		syncYongyou(t){
			this.$api.report.sync(t).then((res) => {
				this.$message({ message: res.msg, type: 'success' });
			}) 
		},
		//导出的方法
		exportExcel(xlsExport) {
			this.filters.pageSize = -1

			this.$api.doi.getWhDoiDetList(this.filters).then((res) => {
				const tHeader = ['单号', '物料编码', '物料名称', '物料规格', '完成数量', '订单时间', '单位'];
				const filterVal = ['doNo', 'code', 'name', 'gg', 'finshCount', 'createTime', 'unit'];
				const list = res.data;
				for (var i = 0; i < list.length; i++) {
					list[i].unit = dictsFormat('units', list[i].unit);
				}
				xlsExport(tHeader, filterVal, list, '入库单明细导出');
			})
		},
		finshHandle: function (index, row) {

	 
				this.$api.doi.finsh(row).then((res) => {
					if (res.code == 200) {
					 	this.findPage(-1);
					} else {
						this.$message({ message: '操作失败, ' + res.msg, type: 'error' })
					}
				})
		 
		},
		selectionFormat: function (row, column, cellValue, index) {
			let key = ""
			let propt = column.property;
			if (propt == "status") {
				key = "doStatus"
			} else {
				key = propt
			}
			return dictsFormat(key, cellValue)
		},
	},
	mounted() {
		this.findPage(1);
	}
}
</script>

<style scoped></style>